package com.hp.attendance.serlvet.oneself;

import com.hp.attendance.entity.Employee;
import com.hp.attendance.entity.Leave;
import com.hp.attendance.service.EmployeeService;
import com.hp.attendance.service.LeaveService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/oneseifAddLeave")
public class OneselfLeaveAddServlet extends HttpServlet {
    private LeaveService leaveService=new LeaveService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            int employee_number = Integer.parseInt(req.getParameter("employeeNumber"));
            int department_number = Integer.parseInt(req.getParameter("departmentNumber"));
            String start1 = req.getParameter("start");
            Date start_time = null;
            try {
                start_time = new SimpleDateFormat("yyyy-MM-dd").parse(start1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            String end1 = req.getParameter("end");
            Date end_time = null;
            try {
                end_time = new SimpleDateFormat("yyyy-MM-dd").parse(end1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            String days = req.getParameter("days");
            String reason = req.getParameter("reason");
            String type = req.getParameter("type");
            Leave leave = new Leave(employee_number, department_number, start_time, end_time, days, reason, type);
            leaveService.addLeave(leave);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
